#include<iostream>
#include<unordered_map>
// zdl:: 正负抵消 + 前缀和 + 哈希表
using namespace std;
const int N = 1e5 + 10;
int sum[N];
unordered_map<int, int> mp;
int main()
{
    int n;
    cin >> n;
    int ret = 0, sum = 0;
    mp[0] = 0;
    for (int i = 1; i <= n; i++)
    {
        int x; cin >> x;
        sum += (x == 0 ? -1 : 1) ;
        if (mp.count(sum)) ret = max(ret, i - mp[sum]);
        else mp[sum] = i;
    }
    cout << ret << endl;
    return 0;
}